package com.bw.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.common.GiteeUtils;
import com.bw.common.JwtUtils;
import com.bw.common.Md5Utils;
import com.bw.entity.User;
import com.bw.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
public class GiteeLoginController {
    @Autowired
    UserMapper userMapper;



    @RequestMapping("/callbackCode.do")
    public String callbackCode(String code, HttpServletResponse response, Model model){

        System.out.println("接收gitee发送过来的临时授权码： " + code);
        // 根据授权码获取 token
        String tokenByCode = GiteeUtils.getAccessTokenByCode(code);
        System.out.println("根据授权码获取到的token" + tokenByCode);
        //根据token 获取用户的资源
        JSONObject userInfoByToken = GiteeUtils.getGiteeUserInfoByToken(tokenByCode);

        System.out.println("获取到的gitee用户信息" +JSON.toJSONString(userInfoByToken));
        // 把gitee账号的id字段 当做自己数据库的giteeid
        Integer giteeId = userInfoByToken.getInteger("id");
        // 把gitee账号的name字段 当做自己数据库的nickName
        String nickName = userInfoByToken.getString("name");
        // 把gitee账号的login字段 当做自己数据库的name
        String name = userInfoByToken.getString("login");
        // 把gitee账号的avatar_url字段 当做自己数据库的avatarUrl
        String avatarUrl = userInfoByToken.getString("avatar_url");

        // 开始执行注册
        // 注册之前 判断一下 之前有没有注册过
        User userFromDB = userMapper.selectByName(name);
        if (userFromDB == null){
            User createUser = new User();
            createUser.setName(name);
            createUser.setGiteeId(giteeId);
            createUser.setNickName(nickName);
            createUser.setAvatarUrl(avatarUrl);
            // 给这个新注册用户设置默认的密码
            String str = "123456";

            String salt = UUID.randomUUID().toString().substring(0, 8);
            createUser.setSalt(salt);
            String md5String = Md5Utils.getMD5String(str + salt);
            createUser.setPassword(md5String);
            // 执行注册
            userMapper.add(createUser);
            // 加密用户信息得到令牌
            String token = JwtUtils.createToken(createUser);
            // 把令牌发到cookie
            Cookie cookie = new Cookie("currentUserToken", token);
            // 把cookie返回给浏览器，让浏览器保存起来
            response.addCookie(cookie);
        }else {
            // 如果之前已经注册过  修改一下 gitee账号的头像和昵称
            userFromDB.setAvatarUrl(avatarUrl);
            userFromDB.setNickName(nickName);
            userMapper.updateById(userFromDB);
            // 加密用户信息得到令牌
            String token = JwtUtils.createToken(userFromDB);
            // 把令牌发到cookie
            Cookie cookie = new Cookie("currentUserToken", token);
            // 把cookie返回给浏览器，让浏览器保存起来
            response.addCookie(cookie);

        }
        model.addAttribute("nickName",nickName);
        model.addAttribute("avatarUrl",avatarUrl);

        return "success";
    }
}
